package cn.tt.tuantuanerp.tuantuanerp.purchase.webapi.controller;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.dto.PurchaseStandardDTO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.PurchaseDetailVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.pojo.vo.PurchaseStandardListVO;
import cn.tt.tuantuanerp.tuantuanerp.comment.security.LoginPrincipal;
import cn.tt.tuantuanerp.tuantuanerp.comment.web.JsonResult;
import cn.tt.tuantuanerp.tuantuanerp.purchase.service.IPurchaseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author zms
 * 采购表控制类
 */
@Slf4j
@RestController
@RequestMapping("/purchases")
@Api(tags = "1.采购订单")

public class PurchaseController {
    @Autowired
    IPurchaseService service;

    /**
     * @author zms
     * 新增采购单
     */

    @ApiOperation ( "新增采购单" )
    @PostMapping("/add")
    public JsonResult addPurchase(@RequestBody PurchaseStandardDTO purchase, @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        service.addPurchase ( purchase,loginPrincipal );
        return  JsonResult.ok ();
    }

    /**
     * @author zms
     * 根据id删除采购订单
     */

    @ApiOperation ( "删除采购单" )
    @PostMapping("/deleteById/{id}")
     public  JsonResult<Void> deleteById(@PathVariable String id){
        service.deletePurchase(id);
         return JsonResult.ok();
     }


     /**
     * @author zms
     * 根据id查询采购订单详情
     */

    @ApiOperation ( "根据id查询采购单详情" )
    @GetMapping("/getById/{id}")
     public  JsonResult getById( @PathVariable String id,@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        PurchaseDetailVO byId = service.getById ( id ,loginPrincipal);
        return JsonResult.ok(byId);
     }


    /**
     * @author zms
     * 查询所有的订单信息
     */
    @GetMapping("/list/{currentPage}/{pageSize}")
    @ApiOperation("采购列表")
    public  JsonResult<List<PurchaseStandardListVO>> list(@PathVariable Integer currentPage, @PathVariable Integer pageSize){
        List<PurchaseStandardListVO> list = service.list(currentPage,pageSize);
        return  JsonResult.ok(list);
    }


    /**
     * @author zms
     * 查询待采购信息
     */
    @GetMapping("/listPrepare/{currentPage}/{pageSize}")
    @ApiOperation("查询待采购信息")
    public  JsonResult<List<PurchaseStandardListVO>> listPrepare(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
       List<PurchaseStandardListVO> purchaseStandardListVOS = service.listPrepare(currentPage,pageSize);
        return  JsonResult.ok(purchaseStandardListVOS);
    }


    /**
     * @author zms
     * 查询采购中信息
     */
    @GetMapping("/listPurchasing/{currentPage}/{pageSize}")
    @ApiOperation("查询采购中信息")
    public  JsonResult<List<PurchaseStandardListVO>> listPurchasing(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        List<PurchaseStandardListVO> purchaseStandardListVOS = service. listPurchasing(currentPage, pageSize );
        return  JsonResult.ok(purchaseStandardListVOS);
    }

    /**
     * @author zms
     * 查询已完成采购信息
     */
    @GetMapping("/listFinished/{currentPage}/{pageSize}")
    @ApiOperation("查询已完成采购信息")
    public  JsonResult<List<PurchaseStandardListVO>> listFinished(@PathVariable Integer currentPage,@PathVariable Integer pageSize){
        List<PurchaseStandardListVO> purchaseStandardListVOS = service.listFinished(currentPage, pageSize );
        return  JsonResult.ok(purchaseStandardListVOS);
    }





    /**
     * @author zms
     * 获取分页数据总数量
     */
    @GetMapping("/totalData/{index}")
    @ApiOperation("获取total数量")
    //判断前端传来的值，0：全部信息，1：待采购，2：采购中，3：已完成
    public  JsonResult totalData(@PathVariable Integer index){

        int i = service.totalData(index);
        return  JsonResult.ok(i);
    }


    /**
     * @author zms
     * 商品的入库设置
     */
    @PostMapping("/stockIn/{purchaseId}")
    @ApiOperation("入库功能")
    public  JsonResult stock(@PathVariable String purchaseId,@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        service.stockIn(purchaseId,loginPrincipal);
        return JsonResult.ok("入库成功");
    }

    /**
     * @author zms
     * 开始采购
     */
    @PostMapping("/purchasing/{purchaseId}")
    @ApiOperation("采购")
    public  JsonResult purchasing(@PathVariable String purchaseId,@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){
        service.purchasing (purchaseId,loginPrincipal);
        return JsonResult.ok("开始采购");
    }


    @PostMapping("/update/{purchaseId}")
    @ApiOperation("修改采购信息")
    public JsonResult updatePurchase(@PathVariable String purchaseId,@ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal){

        return  JsonResult.ok();
    }

}
